/*
 * 基础模块
 * 主要编写常用的方法集,如 字符串去空格 日期格式化...
 */
var Com = (function (w, $) {
  //主对象
  var c = function () {},
    b = c.prototype;
  /**
   * 自定义 投行经典案例弹框
   * @param icon(案例logo图标)
   * @param name(案例公司名称)
   * @param code(案例公司股票代码)
   * @param description(案例相关简介)
   **/
  b.showCaseDetail = function (icon, name, code, description) {
    DialogCase.show({
      title: '温馨提示', //必填
      icon: icon,
      content: '<div class="name">' + name + '(' + code + ')</div>'
        // +'<div class="code">'+code+'</div>'
        +
        '<div class="description">' + description + '</div>', //必填
      leftBtnText: '取消', //选填，默认“取消”
      rightBtnText: '确定', //选填，默认“确认”
      callback: function () { //右边按钮被点击后回调，默认关闭弹框
        console.log('右边按钮被点了');
      }
    });
    //Dialog.hide(); 关闭弹框
  }
  /**
   * 返回後台接口的域名
   * 返回结果 後台接口的域名
   */
  b.getRequestDomain = function () {
    return '';
    // return 'http://172.24.20.210:8082';

  }
  /**
   * 返回後台接口的域名
   * 返回结果 後台接口的域名
   */
  b.getGxwtDomain = function () {
    return 'https://trade2.guosen.com.cn';
    // return 'http://172.24.20.210:8082';

  }
  /**
   * 自定义 ajax
   * @param obj(用来传递参数的对象)
   * @param url(接口地址)
   * @param data(json数据对象)
   * @param method(请求类型)
   * @param success(success)
   * @param isNeedSign(是否需要签名，false或者true)
   * @param isNeedLoading(是否需要加载等待loading效果，false或者true)
   * @param fail(fail)
   **/
  b.ajax = function (obj) {
    var obj = obj || {}
    if (typeof obj.success !== 'function') {
      obj.success = function () {}
    }
    if (typeof obj.error !== 'function') {
      obj.error = function () {}
    }
    if (!obj.method) {
      obj.method = 'POST'
    }
    if (obj.isNeedLoading === undefined) {
      obj.isNeedLoading = false
    }
    if (obj.isNeedCommonData === undefined) {
      obj.isNeedCommonData = true
    }

    obj.data = obj.data || {};

    if (obj.isNeedCommonData) {}

    if (obj.isNeedLoading) {

    }
    $.ajax({
      url: obj.url,
      data: obj.data,
      type: obj.method,
      async: false,
      dataType: 'json',
      contentType: "application/x-www-form-urlencoded;charset=UTF-8",
      success: function (respone) {
        obj.success(respone)
      },
      error: function (XMLHttpRequest, textStatus, errorThrown) {
        alert('网络或者系统出错：' + XMLHttpRequest.status + ',' + XMLHttpRequest.readyState + ',' + textStatus);
      },
    })
  }
  /**
   * 将字符串类型的日期格式化
   * @date 需要格式化的日期，字符串类型
   * @format 格式化后的格式，eg:format="yyyy-MM-dd hh:mm:ss";
   * 返回结果
   */
  b.dateFormat = function (date, format) {
    console.log(date)
    if (typeof date == "string") {
      if (date.indexOf('-') > -1) {
        date = new Date(date.split('-')[0], date.split('-')[1] * 1 - 1, date.split('-')[2]);
      } else if (date.indexOf('/') > -1) {
        date = new Date(date.split('/')[0], date.split('/')[1] * 1 - 1, date.split('/')[2]);
      } else if (date.length == 8) {
        date = new Date(date.substring(0, 4), date.substring(4, 6) * 1 - 1, date.substring(6, 8));
      }
    }
    if (typeof date == "number") {
      date = new Date(date);
    }
    var map = {
      "M": date.getMonth() + 1, //月份
      "d": date.getDate(), //日
      "h": date.getHours(), //小时
      "m": date.getMinutes(), //分
      "s": date.getSeconds(), //秒
      "q": Math.floor((date.getMonth() + 3) / 3), //季度
      "S": date.getMilliseconds() //毫秒
    };
    format = format.replace(/([yMdhmsqS])+/g, function (all, t) {
      var v = map[t];
      if (v !== undefined) {
        if (all.length > 1) {
          v = '0' + v;
          v = v.substr(v.length - 2);
        }
        return v;
      } else if (t === 'y') {
        return (date.getFullYear() + '').substr(4 - all.length);
      }
      return all;
    });
    return format;
  }
  /**
   * 获取业务公告列表，只获取4条
   * @id 公告所在的DOM的id
   * @channelid 栏目id
   * @type 页面样式
   * @count 公告条数，默认4条
   * 返回结果
   */
  b.getNoticeList = function (id, channelid, type, count, url) {
    var count = count ? count : 4;
    var url = url ? url : '/gs/business/info_detail.html'; //?channelid=000100010003000100050001&id=736
    Com.ajax({
      url: Com.getRequestDomain() + '/gswz-web/info/list/1.0',
      data: {
        channelid: channelid
      },
      type: 'post',
      success: function (response) {
        if (response.result[0].code == 0) {
          var html = '',
            list = response.data;
          for (var i = 0, len = list.length > count ? count : list.length; i < len; i++) {
            if (type == 1) {
              html += '<div class="col xs-span-12 span-6">' +
                '<div class="business-notice business-notice-state xs-white serial-num' + (i + 1) + ' serial-num-sp' + (i + 1) + '">' +
                '<div class="business-notice-center">' +
                '<p class="label">' +
                '<span></span>' +
                '</p>' +
                '<div>' +
                '<a href="' + url + '?channelid=' + channelid + '&id=' + list[i].id + '"><span class="business-notice-span1">' + list[i].title + '</a></span>' +
                '<span>' + list[i].publishtime.split(' ')[0] + '</span>' +
                '<span class="business-notice-span2">' + list[i].introduce + '</span>' +
                '</div>' +
                '</div>' +
                '</div>' +
                '</div>';
            } else if (type == 2) {
              html += '<div class="col span-6 xs-span-12 md-span-6">' +
                '<div class="business-notice xs-white serial-type-' + (i + 1) + ' serial-num' + (i + 1) + '">' +
                '<div class="business-notice-center">' +
                '<p class="label">' +
                '<span></span>' +
                '</p>' +
                '<div>' +
                '<a href="' + url + '?channelid=' + channelid + '&id=' + list[i].id + '"><span class="business-notice-span1">' + list[i].title + '</a></span>' +
                '<span class="business-notice-span2">' + list[i].introduce + '</span>' +
                '<span class="business-notice-span3">' + list[i].publishtime.split(' ')[0] + '</span>' +
                '</div>' +
                '</div>' +
                '</div>' +
                '</div>';
            } else if (type == 3) {
              html += '<div class="col span-6 xs-span-12 business-desc">' +
                '<div class="content-wrap row">' +
                '<div class="item col span-2 serial-num' + (i + 1) + '">' +
                '<span></span>' +
                '</div>' +
                '<div class="item col span-8">' +
                '<h3><a href="' + url + '?channelid=' + channelid + '&id=' + list[i].id + '">' + list[i].title + '</a></h3>' +
                '<div class="time">' + list[i].publishtime.split(' ')[0] + '</div>' +
                '<p>' + list[i].introduce + '</p>' +
                '</div>' +
                '</div>' +
                '</div>';
            }
          }
          $('#' + id).html(html);
        } else {
          alert(response.result[0].code + ':' + response.result[0].msg);
        }
      }
    });
  }
  b.navColumn = function (channelid) {
    var channelids = channelid ? channelid : '';
    Com.ajax({
      url: Com.getRequestDomain() + '/gswz-web/channel/location/1.0',
      data: {
        channelid: channelids
      },
      type: 'post',
      success: function (response) {
        if (response.result[0].code == 0) {
          var html = [],
            datas = [];
          datas = response.data;
          for (var i = 0; i < datas.length; i++) {
            if (i < datas.length - 1) {
              html += '<a class="nav-blame-span1" href="' + datas[i].url + '">' + datas[i].name + '</a><i class="iconfont icon-youjiantou"></i>';
            } else {
              html += '<a class="nav-blame-span2" href="' + datas[i].url + '">' + datas[i].name + '</a>';
            }
          }
          console.log(html);
          $('.nav-blame').html(html);
        } else {
          alert(response.result[0].code + ':' + response.result[0].msg);
        }
      }
    });
  }
  /**
   * 获取url参数
   * @param name   //获取name的值
   * @returns {string}
   */
  b.getQueryString = function (name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
    var r = window.location.search.substr(1).match(reg);
    if (r != null) return unescape(r[2]);
    return null;
  }
  /**
   * 获取浏览器的设备信息
   * @returns {string} 1-iphone 2-android 3-windows phone 4-ipad 5-androidpad 6-windowspad 7-微信 8-PC端
   */
  b.browser = function () {
    var ua = navigator.userAgent.toLowerCase();
    if (ua.match(/MicroMessenger/i) == "micromessenger") {
      return 7; //微信返回0
    }

    if (!(/(phone|ipad|ipod|ios|android)/i.test(ua))) { //PC端
      return 8; //PC端返回8
    }

    //userAgent对部分华为的Android判断不准，所以只判断三种情况
    if (ua.indexOf("iphone") > -1) {
      return 1; //iphone手机返回1
    } else if (ua.indexOf("windows phone") > -1) {
      return 3; //windows返回3
    } else {
      return 2; //其他情况都认为是Android
    }
  }
  //设置cookie
  b.setCookie = function (cname, cvalue, exdays) {
    var d = new Date();
    d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
    var expires = "expires=" + d.toUTCString();
    document.cookie = cname + "=" + cvalue + "; " + expires;
  }
  //获取cookie
  b.getCookie = function (cname) {
    var name = cname + "=";
    var ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++) {
      var c = ca[i];
      while (c.charAt(0) == ' ') c = c.substring(1);
      if (c.indexOf(name) != -1) return c.substring(name.length, c.length);
    }
    return "";
  }
  //清除cookie 
  b.clearCookie = function (name) {
    this.setCookie(name, "", -1);
  }
  /*
   获取指定地址里的指定参数
   参数一：地址
   参数二：所要获取的参数
   * */
  b.getParam = function (Url, param) {
    var parm, parm_1, parm_2;
    if (Url.indexOf('?') > -1) {
      parm = Url.split('?');
    } else {
      return;
    }
    if (Url.indexOf('&') > -1) {
      parm_1 = parm[1].split('&');
      for (var i = 0; i < parm_1.length; i++) {
        if (parm_1[i].indexOf(param) > -1) {
          parm_2 = parm_1[i].split('=');
          return parm_2[1];
        }
      }
    } else {
      if (parm[1].indexOf(param) > -1) {
        parm_1 = parm[1].split('=');
        return parm_1[1];
      } else {
        return;
      }
    }
  }
  return new c;
})(window, $)